<?php
require "../config.php";
require "./user_authentication.php";
$user_name = $_POST['id']; //用户名
$Token = $_POST['Token']; //用户token
$userTime = $_POST['userTime']; //用户时间
$status_json = json_decode(user_authentication($user_name,$Token,$userTime),true); //用户登录验证
$file_name = $_POST['file_name'];
if($status_json['status']=="200"){
    $conn = mysqli_connect($MySqlHost, $MySqlUser, $MySqlPwd, $MySqlDatabaseName);
    if (mysqli_connect_errno($conn))
    {
        echo json_encode(array("status"=>"500", "msg" => "连接 MySQL 失败: " . mysqli_connect_error()));
    }
    mysqli_query($conn, "set character set 'utf8'");//读库
    mysqli_query($conn, "set names 'utf8'");//写库
    $res = mysqli_query($conn,"SELECT * FROM files WHERE possessor = '$user_name' AND file_name = '$file_name'");
    if(mysqli_num_rows($res)>0){ //数据库中有文件
        $status=unlink("./Files/" . $file_name); //删除本地文件
        if($status){
            $res = mysqli_query($conn,"DELETE FROM files WHERE possessor = '$user_name' AND file_name = '$file_name'");
            echo json_encode(array("status"=>200,"msg" => "文件删除成功"));
        }else{
            echo json_encode(array("status"=>404,"msg" => "文件删除失败"));
        }
    }else{
        echo json_encode(array("status"=>404,"msg" => "文件不存在"));
    }
    mysqli_close($conn);
}else{
    echo($status_json['status'] . "_" . $status_json['msg'] . "_" . $status_json['time']);
}

